module lang.d.tokens.token;

import lang.d.tokens.tokentype;
import lang.itoken;
import std.variant;

class Token: IToken {
    private const TokenType myTokenType;
    private const uint myBegin;
    private const uint myEnd;
    private Variant myValue;

    /// begin - first token's char, end - char after last token's char
    public this(in TokenType tok, in uint begin, in uint end, Variant value)
    in {
        assert(begin >= 0);
        assert(begin <= end);
        assert(value.hasValue());
    }
    body {
        myTokenType = tok;
        myBegin = begin;
        myEnd = end;
        myValue = value;
    }

    public uint getStartPosition() const {
        return myBegin;
    }
    
    public uint getEndPosition() const {
        return myEnd;
    }

    public TokenType getTokenType() const {
        return myTokenType;
    }

    public T getTokenValue(T)() {
        return myValue.get!T();
    }

    public bool isWhitespace() const {
        return myTokenType == TokenType.WHITESPACE;
    }

    public bool isComment() const {
        return ((myTokenType == TokenType.COMMENT) || (myTokenType == TokenType.DOC_COMMENT));
    }
}
